package org.projectname.service.admin.book;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hishenframework.core.data.jdbc.template.AbstractHishenJdbcTemplate;
import org.hishenframework.core.data.jdbc.template.DataPaging;
import org.hishenframework.core.data.jdbc.template.JdbcOperater;
import org.projectname.entity.book.Book;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 图书业务类
 * @author didi
 *
 */
@Service
@Transactional
public class BookService {
	
	/** jdbc操作类 */
	private JdbcOperater<Book> oper;
	
	@Resource
	public void setJdbcTemplate(AbstractHishenJdbcTemplate<Book> hishenJdbcTemplate) {
		oper = new JdbcOperater<Book>(Book.class, hishenJdbcTemplate);
	}
	
	/**
	 * 添加
	 * @param entity 图书实体
	 * @return 自增主键
	 */
	public long save(Book entity) {
		return oper.save(entity);
	}

	/**
	 * 更新
	 * @param entity 图书实体
	 * @return
	 */
	public int update(Book entity) {
		return oper.update(entity);
	}
	
	/**
	 * 删除
	 * @param id 图书主键
	 * @return
	 */
	public int delete(int id) {
		return oper.delete(id);
	}
	
	/**
	 * 获取一条记录
	 * @param id 图书主键
	 * @return 图书实体
	 */
	public Book get(int id) {
		return oper.get(id);
	}
	
	/**
	 * 查询
	 * @param paging 分页实体
	 * @param bookname 书名
	 * @param from 起始出版日期
	 * @param to 结束出版日期
	 */
	public void search(DataPaging<Book> paging, String bookname, Date from, Date to) {
		StringBuilder sql = new StringBuilder("SELECT * FROM t_book WHERE 1=1");
		List<Object> values = new ArrayList<>();
		if (bookname != null && bookname.length() > 0) {
			sql.append(" AND book_name like ?");
			values.add("%" + bookname + "%");
		}
		if (from != null) {
			sql.append(" AND publication_date > ?");
			values.add(from);
		} 
		if (to != null) {
			sql.append(" AND publication_date < ?");
			values.add(to);
		}
		oper.query().qDataPaging(paging, sql.toString(), values.toArray());
	}
}
